<template>
  <div>
    <van-nav-bar left-arrow @click-left="$router.back()">
      <span slot="title">浏览历史({{ quantity }})</span>
    </van-nav-bar>
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
      :error.sync="error"
      error-text="请求失败，点击重新加载"
    >
      <van-cell
        v-for="(item, index) in list"
        :key="index"
        :title="item.title"
        @click="$router.push({ name: 'article', params: { articleId: item.art_id } })"
      >
        <div slot="label" class="title">
          <span>作者:{{ item.aut_name }}</span>
          <span>{{ item.comm_count }}评论</span>
          <span>{{ item.pubdate | relativeTime }}</span>
        </div>
      </van-cell></van-list
    >
  </div>
</template>

<script>
import { getUserHistory } from '@/api/user.js'

export default {
  name: 'MyHistory',
  data() {
    return {
      list: [], // 用户收藏列表
      page: 2, // 页数
      perPage: 10, // 每页数量
      loading: false,
      finished: false,
      quantity: null,
      error: false
    }
  },
  methods: {
    async onLoad() {
      const { data } = await getUserHistory({
        page: this.page, // 页数
        per_page: this.perPage // 每页数量
      })

      try {
        const { results } = data.data
        this.list.push(...results)
        this.quantity = data.data.total_count

        this.loading = false

        if (results.length) {
          this.page++
        } else {
          this.finished = true
        }
      } catch (error) {
        this.$toast.fail('获取用户浏览历史失败,请重试')
        this.error = true
        this.finished = true
      }
    }
  }
}
</script>

<style lang="less" scoped>
.title {
  span {
    margin: 5px 8px 5px 0;
  }
}
</style>
